﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

using Xunit;
using Xant.FlowEngine.Interface;
using Xant.FlowEngine.Core;
using Xant.FlowEngine.Factory;
using Xant.FlowEngine.Utility;
using Dapper;

namespace Xant.FlowEngine.Tests
{
    public class Tester : IDisposable
    {

        public Tester()
        {
        }

        public void Dispose()
        {
        }

        #region 环境、数据初始化
        private void Init()
        {
            /*MainProcess process = new MainProcess();

            var connString = System.Configuration.ConfigurationManager.ConnectionStrings["XantFlow"].ConnectionString;
            using (var conn = new SqlConnection(connString))
            {
                conn.Query("");
                ownerActivityId = Guid.Empty;
                ownerProcessId = Guid.Empty;
            }
            */
        }
        #endregion

        [Fact]
        public ProcessTemplate TestFlowDefine01()
        {
            var flowDef = new ProcessTemplate("Test on "+DateTime.Now.ToString());
            var start = new StartActivity();
            var end = new EndActivity();
            var auto = new EmailActivity("N2", "发送邮件");
            var manual = new WorkActivity("W1", "经办人处理");
            flowDef.AddActivities(start, end, auto, manual);

            flowDef.AddTransition(start, auto);
            flowDef.AddTransition(auto, manual);
            flowDef.AddTransition(manual, end);
            return flowDef;
        }

        [Fact]
        public void TestCreateProcess01()
        {
            var template = TestFlowDefine01();
            var bagBiz = new BizDataBag() { BillType = "TEST", BillNo = string.Format("TEST{0:yyyyMMdd-HHmm}", DateTime.Now) };
            var process = ProcessManager.Manager.CreateProcessInstance(bagBiz, template);
            ProcessManager.Manager.Run(process);
            var activities = ProcessManager.Manager.GetRunningActivities(process.Id);
            Assert.Equal(activities.Count(), 1);
            var activity = activities.First() as WorkActivity;
            ProcessManager.Manager.WorkitemPass(activity.Workitems.First().Id, null);
        }

        public void TestWorkitemPass()
        {
            Guid workitemId = Guid.Parse(Console.ReadLine());
            Console.WriteLine(workitemId);
        }

        [Fact]
        public void TestSerialize()
        {
            var activity = new EmailActivity() { Code = "N1", Name = "邮件提醒" };
            SerializerManager.Save(activity);
            Console.WriteLine(activity.Id);
        }

        [Fact]
        public void TestCombKey()
        {
            var id = Guid.NewGuid();
            var guid = id.Combine();
            var date = CombKey.GetDateFromComb(guid);
            Console.WriteLine(guid);
            Console.WriteLine(date);
            var guid2 = id.Combine(false);
            Console.WriteLine(guid2);
            var date2 = CombKey.GetDateFromComb(guid2, false);
            Console.WriteLine(date2);
        }

        [Fact]
        public void TestGuid()
        {
            var bytes = new byte[16] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            var guid = new Guid(bytes);
            Console.WriteLine(bytes);
            Console.WriteLine(guid);
        }

        [Fact]
        public void TestAutofac()
        {
            var instance = AutofacDemo.GetInstance();
            var dept = instance.QueryOrganization("00000001-0000-0000-0000-000000000000");
            Assert.Equal(dept.Name, "建筑集团有限公司");
            Assert.Equal(dept.Type, OrganizType.Corporation);
        }
    }
}
